package com.example.location;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.support.v4.app.Fragment;

import com.example.fmgidsprototype.FmGidsPrototypeApp;
import com.example.fmgidsprototype.R;
import com.example.fmgidsprototype.R.id;
import com.example.fmgidsprototype.R.layout;
import com.example.fmgidsprototype.R.menu;
import com.example.fmgidsprototype.data.DataManager;
import com.example.fmgidsprototype.model.Venue;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class ShowMap extends Activity {
	private GoogleMap mMap;
//
    private FmGidsPrototypeApp app;
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_map);
		
		mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();         
		mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);  
		//mMap.setMyLocationEnabled(true);
		
		List<Marker> markers = new ArrayList<Marker>();
		Marker marker;
		
		
        app = (FmGidsPrototypeApp)getApplication();
		DataManager dm = app.getDataManager();
		
		List<Venue> venues = dm.getAllVenuesWithLocations();

		for (Venue v : venues) {
			
			markers.add(mMap.addMarker(new MarkerOptions().position(new LatLng(v.getLatitude(),v.getLongitude())).title(v.getName())));
			
			/*
			marker = mMap.addMarker(new MarkerOptions().position(new LatLng(50.92753, 5.38585)).title(v.getName())); //should be XIOS
			markers.add(marker);
			*/
			
			
			Log.d("bart_name", v.getName());
			Log.d("bart_longitude", String.valueOf(v.getLongitude()));
			Log.d("bart_latitude", String.valueOf(v.getLatitude()));
			Log.d("bart_distfromzero", String.valueOf(v.getDistanceFromZero()));
			Log.d("bart_adres", v.getAddress());
			Log.d("bart_description", v.getDescription());
			Log.d("bart_email", v.getEmail());
			Log.d("bart_rating", String.valueOf(v.getRating()));
			Log.d("bart_markerdefinitie", "einde marker");
		}

		// inside your loop:
	/*	marker = mMap.addMarker(new MarkerOptions().position(new LatLng(50.92753, 5.38585))); //should be XIOS
		markers.add(marker);
		marker.setTitle("xios");
		marker = mMap.addMarker(new MarkerOptions().position(new LatLng(51.92753, 5.30585))); //somewhere in the netherlands
		markers.add(marker);
		marker.setTitle("holland");
		*/
		
		
		
		

		
		// inside your loop:
/*		marker = mMap.addMarker(new MarkerOptions().position(new LatLng(50.92753, 5.38585))); //should be XIOS
		markers.add(marker);
		marker = mMap.addMarker(new MarkerOptions().position(new LatLng(51.92753, 5.30585))); //somewhere in the netherlands
		markers.add(marker);
*/
		LatLngBounds.Builder builder = new LatLngBounds.Builder();
		
		for  (Marker m : markers) {
		    builder.include(m.getPosition());
		}
		
		final LatLngBounds bounds = builder.build();
		final int padding = 10; // offset from edges of the map in pixels
		
		mMap.setOnCameraChangeListener(new OnCameraChangeListener() {

		    @Override
		    public void onCameraChange(CameraPosition arg0) {
		        // Move camera.
		        mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
		        // Remove listener to prevent position reset on camera move.
		        mMap.setOnCameraChangeListener(null);
		    }
		});
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.show_map, menu);
		return true;
	}

}
